﻿using System;

using System.Collections.Generic;
using System.Text;
using System.Data.SqlServerCe;
using System.IO;
using System.Windows.Forms;

namespace ProjetoFase1
{
    static class SyncDB
    {

        public static void createSubscription()
        {

            SqlCeReplication rep = null;

            try
            {
                rep = new SqlCeReplication();

                // This is the connection string to your local SQL Server CE database file
                rep.SubscriberConnectionString = "Data Source=\\My Documents\\" +
                    "g21.sdf;" +
                    "SSCE:Database Password=g21";

                if (!File.Exists("\\My Documents\\g21.sdf"))
                {
                    // Add a subscription and create the local database
                    rep.AddSubscription(AddOption.CreateDatabase); 
                }
            }
            catch (SqlCeException sqlex)
            {
                foreach (SqlCeError sqlError in sqlex.Errors)
                {
                    MessageBox.Show(sqlError.Message, "Error");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            finally
            {
                rep.Dispose();
            }
        }

        public static void dropSubscription()
        {
            SqlCeReplication rep = null;

            try
            {
                rep = new SqlCeReplication();

                // This is the connection string to your local SQL Server CE database file
                rep.SubscriberConnectionString = "Data Source=\\My Documents\\" +
                    "g21.sdf;" +
                    "SSCE:Database Password=g21";

                // If database already exists then...
                if (File.Exists("\\My Documents\\g21.sdf"))
                {
                    // This drops the subscription and deletes the database
                    rep.DropSubscription(DropOption.DropDatabase);
                }
                else
                {
                    MessageBox.Show("You must first create a database", "Error");
                }
            }
            catch (SqlCeException sqlex)
            {
                foreach (SqlCeError sqlError in sqlex.Errors)
                {
                    MessageBox.Show(sqlError.Message, "Error");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            finally
            {
                rep.Dispose();
            }
        }

        public static void synchronizeDatabase(string username, string password)
        {
            SqlCeReplication rep = null;

            try
            {
                rep = new SqlCeReplication();

                // This is the URL that points to sscesa20.dll running on IIS
                rep.InternetUrl = "http://teresa.tagus.ist.utl.pt/g21-A/sqlcesa30.dll";

                // This is your Basic Authentication username
                rep.InternetLogin = @"teresa\"+username;

                // This is your Basic Authentication password
                rep.InternetPassword = password;

                // This is the name of the server running SQL Server
                rep.Publisher = @"teresa\CMOVEL";

                // This is the name of the actual database that you're publishing
                rep.PublisherDatabase = "g21-A-db1";

                // This is the type of authentication you're using
                rep.PublisherSecurityMode = SecurityType.NTAuthentication;

                // This is the name that you give to your publication
                // during the wizard setup
                rep.Publication = "g21-pub";

                // This anonymous subscriber name can be anything you want it to be
                rep.Subscriber = username;

                // This is the connection string to your local SQL Server CE database file
                rep.SubscriberConnectionString = "Data Source=\\My Documents\\" +
                    "g21.sdf;" +
                    "SSCE:Database Password=g21";

                // If database already exists then...
                if (File.Exists("\\My Documents\\g21.sdf"))
                {
                    // Synchronize with SQL Server
                    rep.Synchronize();
                }
                else
                {
                    MessageBox.Show("You must first create a database", "Error");
                }
            }
            catch (SqlCeException sqlex)
            {
                foreach (SqlCeError sqlError in sqlex.Errors)
                {
                    MessageBox.Show(sqlError.Message, "Error");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            finally
            {
                rep.Dispose();
            }

        }

        public static void reinitializeSubscription(string username, string password) {
            
            SqlCeReplication rep = null;

            try
            {
                rep = new SqlCeReplication();

                // This is the URL that points to sscesa20.dll running on IIS
                rep.InternetUrl = "http://teresa.tagus.ist.utl.pt/g21-A/sqlcesa30.dll";

                // This is your Basic Authentication username
                rep.InternetLogin = @"teresa\"+username;

                // This is your Basic Authentication password
                rep.InternetPassword = password;

                // This is the name of the server running SQL Server
                rep.Publisher = @"teresa\CMOVEL";

                // This is the name of the actual database that you're publishing
                rep.PublisherDatabase = "g21-A-db1";

                // This is the type of authentication you're using
                rep.PublisherSecurityMode = SecurityType.NTAuthentication;

                // This is the name that you give to your publication
                // during the wizard setup
                rep.Publication = "g21-pub";

                // This anonymous subscriber name can be anything you want it to be
                rep.Subscriber = username;

                // This is the connection string to your local SQL Server CE database file
                rep.SubscriberConnectionString = "Data Source=\\My Documents\\" +
                    "g21.sdf;" +
                    "SSCE:Database Password=g21";

                // If database already exists then...
                if (File.Exists("\\My Documents\\g21.sdf"))
                {
                    // This uploads your changes and then marks the
                    // subscription for reinitialization
                    rep.ReinitializeSubscription(true);

                    // Synchronize with SQL Server
                    rep.Synchronize();
                }
                else
                {
                    MessageBox.Show("You must first create a database", "Error");
                }
            }
            catch (SqlCeException sqlex)
            {
                foreach (SqlCeError sqlError in sqlex.Errors)
                {
                    MessageBox.Show(sqlError.Message, "Error");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
            finally
            {
                rep.Dispose();
            }
        }
    }
}
